﻿namespace Collaboris.Saf.Adapters
{
    using System;
    using System.Xml;

    using Engine;
    using Engine.Report;

    /// <summary>
    /// The Service Adapter is responsible for communicating with SAF from a 
    /// WCF Services. This allows SAF to be used Remotely across Farms using 
    /// WCF over Http. 
    /// </summary>
    public sealed class ServiceAdapter : AdapterBase
    {
        /// <summary>
        /// Initializes a new instance of the <see cref="ServiceAdapter"/> class.
        /// </summary>
        /// <param name="contextUrl">The context URL.</param>
        /// <param name="processMode">The process mode.</param>
        /// <param name="macroXml">The macro XML.</param>
        public ServiceAdapter(string contextUrl, ProcessMode processMode, string macroXml)
        {
            this.TargetUrl = contextUrl;
            this.Mode = processMode;
            this.xml = macroXml;

            this.instanceId = Guid.NewGuid();
            XmlDocument xd = new XmlDocument();
            xd.LoadXml(macroXml);
            this.instanceFileName = String.Format(@"c:\temp\{0}.macro", this.instanceId);
            xd.Save(this.instanceFileName);
        }

        private readonly Guid instanceId;
        private readonly string instanceFileName;

        private ProcessMode mode;

        private string xml;

        /// <summary>
        /// Gets or sets the macro XML.
        /// </summary>
        /// <value>The macro XML.</value>
        public string MacroXml
        {
            get
            {
                return this.xml;
            }

            set
            {
                this.xml = value;
            }
        }

        #region Overriden Methods

        /// <summary>
        /// Gets or sets the current mode.
        /// </summary>
        /// <value>The current mode.</value>
        public override ProcessMode Mode
        {
            get
            {
                return mode;
            }

            set
            {
                mode = value;
            }
        }

        /// <summary>
        /// Gets or sets the Adapter String for the
        /// current agent.
        /// </summary>
        /// <value>The current directory.</value>
        public override string AdapterName
        {
            get
            {
                return "WCFService";
            }
        }

        /// <summary>
        /// Gets the name of the macro file on disk.
        /// </summary>
        /// <returns></returns>
        protected override string GetMacroName()
        {
            return this.instanceFileName;
        }

        /// <summary>
        /// Called when the Adapter Requires a Macro to be
        /// instatiated and returned.
        /// </summary>
        /// <param name="ctx">The SafContext.</param>
        /// <returns>
        /// An Instantiated Macro, created using Spring.Net
        /// </returns>
        protected override IMacro GetMacro(SafContext ctx)
        {
            // Create the Macro from the string passed in on the Service.

            IMacro newMacro = MacroEngine.CreateFromFile(this.instanceFileName, ctx);

            return newMacro;
        }

        /// <summary>
        /// Called when the Adapter needs to add PlaceHolders
        /// from (its specific environment) to a SAF Macro.
        /// </summary>
        /// <param name="ctx"></param>
        protected override void AddProperties(SafContext ctx)
        {
            
        }

        #endregion

        public MacroReport RunMacro()
        {
            // Calls the Do or the Undo on the Macro
            return ExecuteMacro();
        }
    }
}
